package com.robot.console.util;

import java.util.HashMap;

public class ContextThread extends Thread
{
	private static long id = 0;
	private final HashMap<String, Object> context = new HashMap<String, Object>();

	public ContextThread(Runnable r)
	{
		super(r);
		setName("Context Thread " + id++);
	}

	public void addContext(String name, Object obj) throws IllegalAccessException
	{
		validate();
		context.put(name, obj);
	}

	public boolean containsContext(String name) throws IllegalAccessException
	{
		validate();
		return context.containsKey(name);
	}

	public Object getContext(String name) throws IllegalAccessException
	{
		validate();
		return context.get(name);
	}

	public void removeContext(String name) throws IllegalAccessException
	{
		validate();
		context.remove(name);
	}

	private void validate() throws IllegalAccessException
	{
		if (!Thread.currentThread().getName().equals(getName()))
		{
			throw new IllegalAccessException("Can't access it from other threads");
		}
	}
}
